package com.ihrm.system.controller;

import com.ihrm.common.controller.BaseController;
import com.ihrm.common.entity.PageResult;
import com.ihrm.common.entity.Result;
import com.ihrm.common.entity.ResultCode;
import com.ihrm.domain.system.Role;
import com.ihrm.domain.system.response.RoleResult;
import com.ihrm.system.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    // 给角色分配权限
    @RequestMapping(value = "/role/assignPrem", method = RequestMethod.PUT)
    public Result assignPrem(@RequestBody Map<String,Object> map){
        //1.获取被分配的角色的id
        String roleId = (String) map.get("id");
        //2.获取到权限的id列表
        List<String> permIds = (List<String>) map.get("permIds");
        //3.调用service完成权限分配
        roleService.assignPerms(roleId,permIds);
        return new Result(ResultCode.SUCCESS);
    }

    // 添加角色
    @RequestMapping(value = "/role", method = RequestMethod.POST)
    public Result save(@RequestBody Role role) {
        try {
            role.setCompanyId(companyId);
            roleService.save(role);
            return Result.SUCCESS();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR();
        }

    }

    // 更新角色
    @RequestMapping(value = "/role/{id}", method = RequestMethod.PUT)
    public Result update(@PathVariable(name = "id") String id, @RequestBody Role role) {
        try {
            roleService.update(role);
            return Result.SUCCESS();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR();
        }

    }

    // 删除角色
    @RequestMapping(value = "/role/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable(name = "id") String id) {
        try {
            roleService.delete(id);
            return Result.SUCCESS();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR();
        }

    }

    // 根据id查询角色
    @RequestMapping(value = "/role/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable(name = "id") String id) {
        try {
            Role role = roleService.findById(id);
            RoleResult roleResult = new RoleResult(role);
            return new Result(ResultCode.SUCCESS, roleResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.SERVER_ERROR);
        }
    }

    // 分页查询角色
    @RequestMapping(value = "/role", method = RequestMethod.GET)
    public Result findByPage(int page, int pageSize, @RequestParam Map map) {
        try {
            map.put("companyId", companyId);
            Page<Role> searchPage = roleService.findSearch(map, page, pageSize);
            PageResult<Role> rolePageResult = new PageResult<>(searchPage.getTotalElements(), searchPage.getContent());
            return new Result(ResultCode.SUCCESS, rolePageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    /**
     * 分页查询角色视频编写
     */
    @RequestMapping(value = "/role1", method = RequestMethod.GET)
    public Result findByPage(int page,int pagesize,Role role) throws Exception {
        Page<Role> searchPage = roleService.findByPage(companyId, page, pagesize);
        PageResult<Role> pr = new PageResult(searchPage.getTotalElements(),searchPage.getContent());
        return new Result(ResultCode.SUCCESS,pr);
    }

    @RequestMapping(value="/role/list" ,method=RequestMethod.GET)
    public Result findAll() throws Exception {
        List<Role> roleList = roleService.findAll(companyId);

//        for (Role role:roleList) {
//            System.out.println(role);
//        }
        return new Result(ResultCode.SUCCESS,roleList);
    }

}
